package com.dock.classin.paypal;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.dock.classin.base.DingdanVo;
import com.dock.classin.base.PayPalVo;
import com.dock.classin.base.Paypal;
import com.dock.classin.service.ApiService;
import com.dock.classin.service.PayPalService;
import com.dock.classin.utils.*;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Logger;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/pay/pal")
public class PaypalController {

    @Autowired
    private PayPalService payPalService;
    @Autowired
    private ApiService apiService;

    /**
     * 下单
     */
    @PostMapping("/pay")
    public String pay(@RequestBody PayPalVo payPalVo) throws JSONException {
        if (StringUtils.isEmpty(payPalVo.getDingdan_code())) {
            return "平台订单号不能为空";
        }
        String responseBody=apiService.pay(payPalVo);
        return responseBody;
    }
    /**
     * 支付回调s
     */
    @PostMapping("/zhifuhuidiao")
    public String zhifuhuidiao(HttpServletRequest request) throws IOException   {
        System.out.println("进入回调----------------------------------------------------");
        String success=apiService.zhifuhuidiao(request);
        return success;
    }
    /**
     * 查询订单
     */
    @GetMapping("/query")
    public AjaxResult query(String dingdan_code) {
        Paypal paypal = payPalService.selectPaypalBydingdanId(dingdan_code);
        if (paypal != null) {
            DingdanVo dingdanVo = new DingdanVo();
            dingdanVo.setDingdan_code(dingdan_code);
            dingdanVo.setStatus(paypal.getStatus());
            return AjaxResult.success(dingdanVo);
        } else {
            return AjaxResult.error("订单号为空或错误");
        }
    }
    /**
     * 退款
     */
    @GetMapping("/refund")
    public AjaxResult refund(String dingdan_code) throws JSONException {
        Paypal paypal = payPalService.selectPaypalBydingdanId(dingdan_code);
        DingdanVo dingdanVo=new DingdanVo();
        if (paypal != null) {
           String responseBody=apiService.refund(dingdan_code,paypal);
            com.alibaba.fastjson.JSONObject jsonObject =  JSON.parseObject(responseBody);
            String refundStatus=jsonObject.get("refundStatus").toString();
            if (StringUtils.isEmpty(refundStatus)){
                dingdanVo.setStatus("退款失败");
            }else {
                if ("SUCCESS".equals(refundStatus)){
                    dingdanVo.setStatus("已退款");
                }else {
                    dingdanVo.setStatus("退款失败");
                }
            }
            dingdanVo.setDingdan_code(dingdan_code);
            return AjaxResult.success(dingdanVo);
        }else {
            dingdanVo.setStatus("退款失败");
            dingdanVo.setDingdan_code(dingdan_code);
            return AjaxResult.success(dingdanVo);
        }
    }

    /**
     * 退款查询
     */
    @GetMapping("/refundquery")
    public String refund_query(String merOrderId, String targetOrderId) throws JSONException {
       String responseBody=apiService.refund_query(merOrderId,targetOrderId);
        return responseBody;
    }
}
